import { Component, OnInit } from '@angular/core';
import {NgbActiveModal, NgbModalRef} from '@ng-bootstrap/ng-bootstrap';
import { JhiEventManager } from 'ng-jhipster';
import { Account, LoginModalService, Principal } from '../shared';
import {Record} from "../entities/record/record.model";
import {RecordService} from "../entities/record/record.service";
import {HttpErrorResponse, HttpResponse} from "@angular/common/http";
import {Observable} from "rxjs/Observable";

@Component({
    selector: 'jhi-home',
    templateUrl: './home.component.html',
    styleUrls: [
        'home.css'
    ]

})
export class HomeComponent implements OnInit {

    record: Record = new Record();
    isSaving: boolean;
    isTip: boolean;
    isError: boolean;
    isclose: boolean;

    account: Account;
    modalRef: NgbModalRef;

    constructor(
        private principal: Principal,
        private loginModalService: LoginModalService,
        private eventManager: JhiEventManager,
        private recordService: RecordService,
        public activeModal: NgbActiveModal,

    ) {
    }

    ngOnInit() {
        this.record.equipment = '电脑';
        this.isTip = false;
        this.isSaving = false;
        this.isError = false;
        this.isclose = false;

         this.principal.identity().then((account) => {
            this.account = account;
        });
         this.registerAuthenticationSuccess();


    }

    clear() {
        this.record = new Record();
        this.isclose = true;
        window.setTimeout(() => {
            this.isclose = false;
        }, 2000);
    }

    save() {
        const myDate = new Date();
        console.log('*************', myDate);
        console.log('*************', this.record);

        this.isSaving = true;
        if (this.record.id !== undefined) {
            this.subscribeToSaveResponse(
                this.recordService.update(this.record));
        } else {
            this.subscribeToSaveResponse(
                this.recordService.create(this.record));
        }
    }
    private subscribeToSaveResponse(result: Observable<HttpResponse<Record>>) {
        result.subscribe((res: HttpResponse<Record>) =>
            this.onSaveSuccess(res.body), (res: HttpErrorResponse) => this.onSaveError());
    }

    private onSaveSuccess(result: Record) {
        this.eventManager.broadcast({ name: 'recordListModification', content: 'OK'});
        this.isSaving = false;
        this.record = new Record();
        this.activeModal.dismiss(result);
        this.isTip = true;
        window.setTimeout(() => {
            this.isTip = false;
        }, 2000);
    }

    private onSaveError() {
        this.isSaving = false;
        this.isError = true;
        window.setTimeout(() => {
            this.isError = false;
        }, 2000);

    }

    registerAuthenticationSuccess() {
        this.eventManager.subscribe('authenticationSuccess', (message) => {
            this.principal.identity().then((account) => {
                this.account = account;
            });
        });
    }

     isAuthenticated() {
        return this.principal.isAuthenticated();
    }

     login() {
        this.modalRef = this.loginModalService.open();
    }


}
